﻿using AlphaBeatSoundEngine;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace KinectAlphaBeat
{

    //StartupUri="MainWindow.xaml" XAML code
    /// <summary>
    /// Logique d'interaction pour App.xaml
    /// </summary>
    public partial class Game : Application
    {

        private MainWindow _mainWindow;
        private KinectDrawer.Drawer _drawer;
        private AlphaBeat _alphaBeat;
        private DrawingAnalyzer _analyzer;

        private string _word;

        public void app_Startup(object sender, StartupEventArgs e)
        {

            _word = String.Empty;

            _alphaBeat = new AlphaBeat();
            _drawer = new KinectDrawer.Drawer();

            _mainWindow = new MainWindow();
            _analyzer = new DrawingAnalyzer();

            _drawer.RealtimeDrawingReady += _mainWindow.DrawerSentNewRealtimePicture;
            _drawer.UserDrawingReady += _analyzer.DrawerFinishedDrawing;
            _analyzer.DrawingAnalyzerFoundSymbol += this.AnalyzerFinishedFoundUserCommand;
            _alphaBeat.AlphaBeatWillPlayLetter += _mainWindow.AlphaBeatChangedLetter;


            // Open a window
            _mainWindow.Show();

            

            _drawer.StartSensors();
        }

        private void AnalyzerFinishedFoundUserCommand(object sender, DrawingAnalyzerFoundSymbolEventArgs args)
        {
            switch (args.UserAction.UserActionType)
            {
                case UserActionType.Command:
                    ExecuteCommand(args.UserAction.Command);
                    break;
                case UserActionType.Letter:
                    AddLetter(args.UserAction.Letter);
                    break;
                case UserActionType.Unknown:
                    break;
            }

            if (!_word.Equals(String.Empty) && !_alphaBeat.IsStarted)
            {
                _alphaBeat.Start();
            }
            else if (_word.Equals(String.Empty))
            {
                _alphaBeat.Stop();
            }
        }

        private void ExecuteCommand(UserActionCommand command)
        {
            switch (command)
            {
                case UserActionCommand.EreaseWord:
                    _word = String.Empty;
                    _alphaBeat.DefineWord(_word);
                    _mainWindow.DisplayWord(_word);
                    break;
                case UserActionCommand.RemoveLastLetter:
                    _word = _word.Substring(0, _word.Length - 1);
                    _alphaBeat.DefineWord(_word);
                    _mainWindow.DisplayWord(_word);
                    break;
            }
        }

        private void AddLetter(Char letter)
        {
            _word += letter;
            _alphaBeat.DefineWord(_word);
            _mainWindow.DisplayWord(_word);
        }

        private void DisplayUnknownActionMessage()
        {
            //TODO
        }


    }


}
